package com.bajiao.admin.controller.order;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bajiao.common.api.vo.Result;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.entity.order.Order;
import com.bajiao.service.order.OrderService;
import com.bajiao.vo.order.OrderAdmin;
import com.bajiao.vo.user.UserAdmin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 订单表
 * @Author: FanJq
 * @Date: 2024-12-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "订单表")
@RestController
@RequestMapping("/order")
public class OrderController extends AdminBaseController<Order, OrderService> {
    @Autowired
    private OrderService orderService;

    /**
     * @return
     * @description: 分页列表查询
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-分页列表查询", notes = "订单表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String orderNo, String keywords, String productTitle, Integer status, String createTimeBegin, String createTimeEnd) {
        startPage();
        List<OrderAdmin> resultList = orderService.getAdminList(orderNo, keywords, productTitle, status, createTimeBegin, createTimeEnd);
        return Result.ok(getDataTable(resultList));
    }

    @ApiOperation(value = "订单数据统计", notes = "订单数据统计")
    @GetMapping(value = "/stat")
    public Result<?> getOrderStat() {
        Map<String, Object> result = orderService.getOrderStat();
        return Result.ok(result);
    }

    /**
     * @param order
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-添加", notes = "订单表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Order order) {
        orderService.save(order);
        return Result.ok("添加成功！");
    }

    /**
     * @param order
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-编辑", notes = "订单表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Order order) {
        orderService.updateById(order);
        return Result.ok("编辑成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-通过id删除", notes = "订单表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        orderService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-批量删除", notes = "订单表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.orderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2024-12-14
     */
    @ApiOperation(value = "订单表-通过id查询", notes = "订单表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Order order = orderService.getById(id);
        return Result.ok(order);
    }

	/**
	 * @Description
	 * 导出excel
	 * @date 2025/1/7 17:51
	 * @author FanJq
	 * @param orderNo
	 * @param keywords
	 * @param productTitle
	 * @param status
	 * @param createTimeBegin
	 * @param createTimeEnd
	 * @param fileName
	 * @return {@link ModelAndView}
	*/
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(String orderNo, String keywords, String productTitle, Integer status, String createTimeBegin, String createTimeEnd, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.2 获取导出数据
        List<OrderAdmin> exportList = orderService.getAdminList(orderNo, keywords, productTitle, status, createTimeBegin, createTimeEnd);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, OrderAdmin.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * @param request
     * @param response
     * @return
     * @description: 通过excel导入数据
     * @author: FanJq
     * @date: 2024-12-14
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Order.class);
    }

}
